package com.wang.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wang.conf.AppConf;
import com.wang.wxpojo.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author wcy
 * @date 2023-02-10 17:57
 */
public class WxMsgUtil2 {


    static  RestTemplate restTemplate = getRestTemplate();


    public static RestTemplate getRestTemplate() {
        RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory());
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    public static ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(30000);//restTemplate 请求超时时间
        factory.setConnectTimeout(30000);// 连接超时
        return factory;
    }

   /* static String token = "";
    static String[] to_openid_list = {"onVjS4i4kSclRe-vj1MRI8gxXm4U"};  //测试使用
    static String mp_template_id = AppConf.template_id;
    static String sn = AppConf.sn;
    static String modelId = AppConf.modelId;
    static String page = AppConf.page;*/


   /* public static void main(String[] args) {
        System.out.println(getToken());
    }*/
    public static void main(String[] args) {



        WxDataPack3 wxDataPack3 = new WxDataPack3();
        wxDataPack3.setTemplate_id("1SttDhyJ9j3OcTtmni6q07FRZEB8rr14FMPzYSbxevM");
        wxDataPack3.setMiniprogram_state("formal");
        wxDataPack3.setTouser("owzGn4rqD9yzVgCz6-PuYPv97tmU");
        wxDataPack3.setLang("zh_CN");

        MsgData data = new MsgData();

        data.setTime1("2022年1月4日 14:00:00");
        data.setEnum_String2("水温");
        data.setEnum_String3("偏高");
        data.setEnum_String4("请及时查看数据");

        wxDataPack3.setData(JSON.toJSONString(data));

        sendWxData(JSON.toJSON(wxDataPack3).toString());
        System.out.println(JSON.toJSON(wxDataPack3));

      /*  String data = "{\n" +
                "  \"touser\": \"owzGn4rqD9yzVgCz6-PuYPv97tmU\",\n" +
                "  \"template_id\": \"1SttDhyJ9j3OcTtmni6q07FRZEB8rr14FMPzYSbxevM\",\n" +
                "  \"page\": \"index\",\n" +
                "  \"miniprogram_state\":\"developer\",\n" +
                "  \"lang\":\"zh_CN\",\n" +
                "  \"data\": {\n" +
                "      \"time1\": {\n" +
                "          \"value\": \"2022年1月4日 14:00:00\"\n" +
                "      },\n" +
                "      \"enum_string2\": {\n" +
                "          \"value\": \"温度\"\n" +
                "      },\n" +
                "      \"enum_string3\": {\n" +
                "          \"value\": \"28.5\"\n" +
                "      } ,\n" +
                "      \"enum_string4\": {\n" +
                "          \"value\": \"请及时查看数据\"\n" +
                "      }\n" +
                "  }\n" +
                "} ";
        sendWxData(data);*/
     /*   String data = "{\n" +
                "  \"touser\": \"owzGn4rqD9yzVgCz6-PuYPv97tmU\",\n" +
                "  \"template_id\": \"BLa3FAd_DLpum5wTffKKWQxJn_dPtrEWcEu1e9z2rC8\",\n" +
                "  \"page\": \"index\",\n" +
                "  \"miniprogram_state\":\"developer\",\n" +
                "  \"lang\":\"zh_CN\",\n" +
                "  \"data\": {\n" +
                "      \"character_string1\": {\n" +
                "          \"value\": \"x123456\"\n" +
                "      },\n" +
                "      \"thing2\": {\n" +
                "          \"value\": \"XA001\"\n" +
                "      },\n" +
                "      \"thing3\": {\n" +
                "          \"value\": \"互通街道\"\n" +
                "      } ,\n" +
                "      \"thing4\": {\n" +
                "          \"value\": \"数据超标\"\n" +
                "      } ,\n" +
                "      \"thing4\": {\n" +
                "          \"value\": \"数据超标\"\n" +
                "      } ,\n" +
                "      \"thing5\": {\n" +
                "          \"value\": \"测试\"\n" +
                "      }\n" +
                "  }\n" +
                "} ";
        sendWxData(data);*/
      /*  String data = "{\n" +
                "  \"model_id\": \"AumPHSraCOEfP-znzqEelw\",\n" +
                "  \"group_name\": \"test1\"} ";
        sendWxData(data);*/
    /*    String data = "{\n" +
                "  \"touser\": \"owzGn4rqD9yzVgCz6-PuYPv97tmU\",\n" +
                "  \"template_id\": \"cf_vuRwOzyFaMFjyafBrZnCEVzzCsVsqI1SLtdblHm4\",\n" +
                "  \"page\": \"index\",\n" +
                "  \"miniprogram_state\":\"developer\",\n" +
                "  \"lang\":\"zh_CN\",\n" +
                "  \"data\": {\n" +
                "      \"time1\": {\n" +
                "          \"value\": \"2022年1月4日 14:00:00\"\n" +
                "      },\n" +
                "      \"character_string2\": {\n" +
                "          \"value\": \"10001\"\n" +
                "      },\n"+
                "      \"enum_string4\": {\n" +
                "          \"value\": \"请及时查看数据异常\"\n" +
                "      }\n" +
                "  }\n" +
                "} ";
        sendWxData(data);*/

    }

/*
    public static void main(String[] args) {

        String wxData = "{\"mp_0000008b5a4c7b3513da7d\",\"data\":{\"address\":{\"value\":\"未设置地址\"},\"first\":{\"value\":\"您好:LH-DZ_test69211监测数据异常\"},\"projectName\":{\"color\":\"#173177\",\"value\":\"氨氮;\"},\"remark\":{\"value\":\"如查看详细数据，点击进入小程序查看，如有疑问，请联系客服\"},\"result\":{\"color\":\"#FF0000\",\"value\":\"氨氮:0.40,偏低\\n\\n\"},\"time\":{\"value\":\"2023-05-12 15:43:35\"}},\"miniprogram\":{\"appid\":\"wxbc5ccf8ddfeaf689\",\"pagepath\":\"pages/home/index\"},\"template_id\":\"M5BgZ92EjqQVxcUK8Rx_Xo2ivVVl_kFjxgN6vdJVzEU\",\"url\":\"http://weixin.qq.com/download\"},\"touser\":\"owzGn4uUl0KLYq4yJsTmQKRnye-c\"}";
        sendWxData(wxData);
    }
    */



    //发送微信公众号通知
    public static  String  sendWxData(String wxData){
        String access_token = getToken();

        System.out.println(access_token);
        String sendDataUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+access_token;
        String result =  restTemplate.postForObject(sendDataUrl,wxData,String.class);


        System.out.println("返回结果："+result);
        JSONObject jsonObject =  JSON.parseObject(result,JSONObject.class);
        String resultCode = jsonObject.get("errcode").toString();
        if(resultCode.equals("0")){
            return "success";

        }else {
            return "error";
        }

    }


    public static String  getMyAppToken(){

        /*Long nowTime = new Date().getTime();


        if(EmptyUtil.isNotNull(AppConf.access_token) && (nowTime-AppConf.getAccess_tokenTime)<7100){

            return AppConf.access_token;
        }else {
            AppConf.access_token = getToken();
            AppConf.getAccess_tokenTime = new Date().getTime();
            return AppConf.access_token;
        }*/
        return null;
    }


    public static String getToken() {

        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+AppConf.weAppId+"&secret="+AppConf.weAppSecret;

        ResponseEntity<String> forEntity = restTemplate.getForEntity(tokenUrl, String.class, new Object[0]);
        String body = (String)forEntity.getBody();

        JSONObject jsonObject1 = JSON.parseObject(body,JSONObject.class);


        return jsonObject1.get("access_token").toString();

    }

}
